1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.Beta;
20  import com.google.common.annotations.GwtCompatible;
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  import javax.annotation.Nullable;
28  
29  /**
30   * A list which forwards all its method calls to another list. Subclasses should
31   * override one or more methods to modify the behavior of the backing list as
32   * desired per the <a
33   * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
34   *
35   * <p>This class does not implement {@link java.util.RandomAccess}. If the
36   * delegate supports random access, the {@code ForwardingList} subclass should
37   * implement the {@code RandomAccess} interface.
38   *
39   * <p><b>Warning:</b> The methods of {@code ForwardingList} forward
40   * <b>indiscriminately</b> to the methods of the delegate. For example,
41   * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
42   * #addAll}, which can lead to unexpected behavior. In this case, you should
43   * override {@code addAll} as well, either providing your own implementation, or
44   * delegating to the provided {@code standardAddAll} method.
45   *
46   * <p>The {@code standard} methods and any collection views they return are not
47   * guaranteed to be thread-safe, even when all of the methods that they depend
48   * on are thread-safe.
49   *
50   * @author Mike Bostock
51   * @author Louis Wasserman
52   * @since 2.0 (imported from Google Collections Library)
53   */
54  @GwtCompatible
55  public abstract class ForwardingList<E> extends ForwardingCollection<E>
56      implements List<E> {
57    // TODO(user): identify places where thread safety is actually lost
58  
59    /** Constructor for use by subclasses. */
60    protected ForwardingList() {}
61  
62    @Override protected abstract List<E> delegate();
63  
64    @Override
65    public void add(int index, E element) {
66      delegate().add(index, element);
67    }
68  
69    @Override
70    public boolean addAll(int index, Collection<? extends E> elements) {
71      return delegate().addAll(index, elements);
72    }
73  
74    @Override
75    public E get(int index) {
76      return delegate().get(index);
77    }
78  
79    @Override
80    public int indexOf(Object element) {
81      return delegate().indexOf(element);
82    }
83  
84    @Override
85    public int lastIndexOf(Object element) {
86      return delegate().lastIndexOf(element);
87    }
88  
89    @Override
90    public ListIterator<E> listIterator() {
91      return delegate().listIterator();
92    }
93  
94    @Override
95    public ListIterator<E> listIterator(int index) {
96      return delegate().listIterator(index);
97    }
98  
99    @Override
100   public E remove(int index) {
101     return delegate().remove(index);
102   }
103 
104   @Override
105   public E set(int index, E element) {
106     return delegate().set(index, element);
107   }
108 
109   @Override
110   public List<E> subList(int fromIndex, int toIndex) {
111     return delegate().subList(fromIndex, toIndex);
112   }
113 
114   @Override public boolean equals(@Nullable Object object) {
115     return object == this || delegate().equals(object);
116   }
117 
118   @Override public int hashCode() {
119     return delegate().hashCode();
120   }
121 
122   /**
123    * A sensible default implementation of {@link #add(Object)}, in terms of
124    * {@link #add(int, Object)}. If you override {@link #add(int, Object)}, you
125    * may wish to override {@link #add(Object)} to forward to this
126    * implementation.
127    *
128    * @since 7.0
129    */
130   protected boolean standardAdd(E element) {
131     add(size(), element);
132     return true;
133   }
134 
135   /**
136    * A sensible default implementation of {@link #addAll(int, Collection)}, in
137    * terms of the {@code add} method of {@link #listIterator(int)}. If you
138    * override {@link #listIterator(int)}, you may wish to override {@link
139    * #addAll(int, Collection)} to forward to this implementation.
140    *
141    * @since 7.0
142    */
143   protected boolean standardAddAll(
144       int index, Iterable<? extends E> elements) {
145     return Lists.addAllImpl(this, index, elements);
146   }
147 
148   /**
149    * A sensible default implementation of {@link #indexOf}, in terms of {@link
150    * #listIterator()}. If you override {@link #listIterator()}, you may wish to
151    * override {@link #indexOf} to forward to this implementation.
152    *
153    * @since 7.0
154    */
155   protected int standardIndexOf(@Nullable Object element) {
156     return Lists.indexOfImpl(this, element);
157   }
158 
159   /**
160    * A sensible default implementation of {@link #lastIndexOf}, in terms of
161    * {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you
162    * may wish to override {@link #lastIndexOf} to forward to this
163    * implementation.
164    *
165    * @since 7.0
166    */
167   protected int standardLastIndexOf(@Nullable Object element) {
168     return Lists.lastIndexOfImpl(this, element);
169   }
170 
171   /**
172    * A sensible default implementation of {@link #iterator}, in terms of
173    * {@link #listIterator()}. If you override {@link #listIterator()}, you may
174    * wish to override {@link #iterator} to forward to this implementation.
175    *
176    * @since 7.0
177    */
178   protected Iterator<E> standardIterator() {
179     return listIterator();
180   }
181 
182   /**
183    * A sensible default implementation of {@link #listIterator()}, in terms of
184    * {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you
185    * may wish to override {@link #listIterator()} to forward to this
186    * implementation.
187    *
188    * @since 7.0
189    */
190   protected ListIterator<E> standardListIterator() {
191     return listIterator(0);
192   }
193 
194   /**
195    * A sensible default implementation of {@link #listIterator(int)}, in terms
196    * of {@link #size}, {@link #get(int)}, {@link #set(int, Object)}, {@link
197    * #add(int, Object)}, and {@link #remove(int)}. If you override any of these
198    * methods, you may wish to override {@link #listIterator(int)} to forward to
199    * this implementation.
200    *
201    * @since 7.0
202    */
203   @Beta protected ListIterator<E> standardListIterator(int start) {
204     return Lists.listIteratorImpl(this, start);
205   }
206 
207   /**
208    * A sensible default implementation of {@link #subList(int, int)}. If you
209    * override any other methods, you may wish to override {@link #subList(int,
210    * int)} to forward to this implementation.
211    *
212    * @since 7.0
213    */
214   @Beta protected List<E> standardSubList(int fromIndex, int toIndex) {
215     return Lists.subListImpl(this, fromIndex, toIndex);
216   }
217 
218   /**
219    * A sensible definition of {@link #equals(Object)} in terms of {@link #size}
220    * and {@link #iterator}. If you override either of those methods, you may
221    * wish to override {@link #equals(Object)} to forward to this implementation.
222    *
223    * @since 7.0
224    */
225   @Beta protected boolean standardEquals(@Nullable Object object) {
226     return Lists.equalsImpl(this, object);
227   }
228 
229   /**
230    * A sensible definition of {@link #hashCode} in terms of {@link #iterator}.
231    * If you override {@link #iterator}, you may wish to override {@link
232    * #hashCode} to forward to this implementation.
233    *
234    * @since 7.0
235    */
236   @Beta protected int standardHashCode() {
237     return Lists.hashCodeImpl(this);
238   }
239 }